﻿<div class="page-bar">
    <ul class="page-breadcrumb">
        <li>
            <span id="module">佛吉亚管理</span>
            <i class="fa fa-angle-right"></i>
        </li>
        <li>
            <span id="action">库存列表</span>
        </li>
    </ul>
</div>
<!-- END PAGE BAR -->
<div class="mt-bootstrap-tables" style="margin-top: 20px">
    <div class="row">
        <div class="col-md-12">
            <div class="portlet light bordered">
                @*<div class="portlet-title">
                        <div class="caption">
                            <i class="icon-social-dribbble font-green hide"></i>
                        </div>
                        <div class="actions">
                            <a class="btn btn-default" href="javascript:Edit(null);">
                                <i class="fa fa-plus"></i>创建
                            </a>
                        </div>
                    </div>*@
                <div class="portlet-body">
                    <!-- BEGIN FORM-->
                    @*<form id="defaultForm" class="form-horizontal" method="post" novalidate="novalidate">
                            <div class="mt-checkbox-inline" style="margin-top: -37px">
                                      <div class="pull-left" style="margin-top: 20px">
                                          状态：
                                          <select class="form-control input-inline" name="Deleted" id="State">
                                              <option value="">全部</option>
                                              <option value="1">正常</option>
                                              <option value="0">禁用</option>
                                          </select>
                                      </div>
                                  </div
                        </form>*@
                    <!-- END FORM-->
                    <table id="table-pagination" data-toggle="table" data-url="@Url.Action("GetProductInfoPagedList", new { ViewBag.ProductTypeId })"
                           data-height="526"
                           data-form-id="defaultForm"
                           data-show-refresh="true"
                           data-page-size="10"
                           data-page-list="[5, 10, 20, 50, 100, 200]"
                           data-auto-height="true"
                           data-search="true">
                        <thead>
                            <tr>
                                <th data-formatter="RowNumber" data-width="50">序号</th>
                                <th data-field="Name">产品</th>
                                @if (Model != null && Model.Count > 0)
                                {
                                    foreach (var productType in Model)
                                    {
                                        <th keyid="@productType.KeyId" data-field=@("PropertyDictionary."+productType.KeyId)>@productType.Key</th>
                                    }
                                }
                                <th data-align="right" data-field="ProductCount" data-width="100">产品数</th>
                                <th data-align="right" data-formatter="ShowCarCount" data-width="100">车数</th>
                                <th data-align="right" data-field="ProductMinimumCount" data-width="100">预警数</th>
                                <th data-align="center" data-formatter="opColumns" data-width="220">操作</th>
                            </tr>
                        </thead>
                    </table>
                </div>
            </div>

            <!-- 参数设置 弹出框模板 -->
            <div id="dialogTemplet" class="hide">
                <form class="form-horizontal" method="post">
                    <div class="form-body">
                        <div class="row">
                            <div class="col-md-10">
                                <div class="form-group">
                                    <label class="control-label col-md-2">产品</label>
                                    <div class="col-md-10">
                                        <input type="hidden" id="ProductId" name="ProductId" />
                                        <input type="hidden" id="OldCount" name="OldCount" />
                                        <p class="form-control-static lblName"> 产品 </p>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-10">
                                <div class="form-group">
                                    <label class="control-label col-md-2">车号</label>
                                    <div class="col-md-10">
                                        @*<select class="form-control" id="CarKey" name="CarKey" required>*@
                                        <select id="CarKey" name="CarKey" class="bs-select form-control asdfasdfwwww" data-live-search="true" data-size="8" onchange="onChange($(this))" required></select>
                                    </div>
                                </div>
                            </div>
                        </div>
                        @*<div class="row">
                                <div class="col-md-10">
                                    <div class="form-group">
                                        <label class="control-label col-md-2">入库方式</label>
                                        <div class="col-md-10">
                                            <select class="form-control" id="Operation" name="Operation">
                                                <option value="3">盘盈</option>
                                                <option value="4">盘亏</option>
                                            </select>
                                        </div>
                                    </div>
                                </div>
                            </div>*@
                        <div class="row">
                            <div class="col-md-10">
                                <div class="form-group">
                                    <label class="control-label col-md-2">产品数</label>
                                    <div class="col-md-10">
                                        <input type="text" name="ProductCount" class="form-control ProductCount asdfasdfasdf" autocomplete="off" onkeyup="inputInt(this)" value="0" required />
                                    </div>
                                </div>
                            </div>
                        </div>
                        @*<div class="row">
                                <div class="col-md-10">
                                    <div class="form-group">
                                        <label class="control-label col-md-2">车数</label>
                                        <div class="col-md-10">
                                            <input type="text" name="CarCount" class="form-control" autocomplete="off" onkeyup="inputInt(this)" value="0" required />
                                        </div>
                                    </div>
                                </div>
                            </div>*@
                    </div>
                </form>
            </div>
            <!-- End 弹出框模板 -->

        </div>
    </div>
</div>

@section scripts{
    <script type="text/javascript">

        var carList = @Html.Raw(ViewBag.CarListJson);

        $(function(){
            //$('.bs-select').selectpicker({
            //    iconBase: 'fa',
            //    tickIcon: 'fa-check'
            //});
        });

        var onChange = function(obj){
            var value = obj.find('option:selected').attr('productcount');
            $('#OldCount,.ProductCount').val(value);
        };

        var opColumns = function(value, row, index) {
            var dataModel = escape(JSON.stringify(row));

            var html = `<a href="@Url.Action("Log")?pid=_id_">库存记录</a>
                | <a onclick="showDialog('` + dataModel + `')" href="javascript:void(0)">调整库存</a>`;

            return html.replace(/_id_/ig, row.Id);
        };

        var  ShowCarCount = function (value, row, index) {
            return row.ProductMinimumCount !== 0 && row.CarCount <= row.ProductMinimumCount
                ? '<span style="color:red">' + row.CarCount + '</span>'
                : row.CarCount;
        };

        var updateCarJson = function(id, count){
            $.each(carList, function(index, data) {
                if(data.Key == id){
                    data.ProductCount = count;
                }
            });
        };

        var showDialog = function(parameter) {
            var dataModel = [];
            var productCount = 0;

            if (parameter.length !== undefined) {
                dataModel = eval('(' + unescape(parameter) + ')');

                $('#ProductId').val(dataModel.Id);
                $('#CarKey,ul.dropdown-menu').html('');

                $.each(carList, function(index, data) {
                    if (data.Status == 1 && data.ProductId == dataModel.Id) { //模拟入库时，只显示已出库数据
                        $('#CarKey').append('<option value="' + data.Key + '" productCount="' + data.ProductCount + '">' + data.Number + '</option>');

                        if(productCount == 0) productCount= data.ProductCount;
                    }
                });
            }

            var settingParamDialog = bootbox.dialog({
                title: '库存调整',
                message: $('#dialogTemplet').html(),
                className: 'my-modal',
                buttons: {
                    success: {
                        label: '取 消'
                    },
                    main: {
                        label: '确 定',
                        callback: function() {
                            var formDoc = $('.my-modal .modal-body form');
                            formDoc.submit();
                            return false;
                        }
                    }
                }
            });

            $('#OldCount,.ProductCount').val(productCount);

            //初始化弹出框
            settingParamDialog.init(function() {
                var formDoc = $('.my-modal .modal-body form');

                if (parameter.length !== undefined) {
                    //dataModel = eval('(' + unescape(parameter) + ')');

                    $('.lblName', formDoc).html(dataModel.FullName);
                }

                customFormValidate(formDoc, {}, function(form) {
                    var data = $(form).serializeObject();
                    $.ajax({
                        type: 'POST',
                        url: '@Url.Action("AdjustProductCount")',
                        data: data,
                        async: false,
                        success: function(reslut) {
                            if (reslut.Success) {
                                bootbox.hideAll(); //关闭弹出框
                                showToast(0, '系统提示', '保存成功');
                                updateCarJson(data.CarKey, data.ProductCount);
                                $('#table-pagination').bootstrapTable('refresh');
                            } else {
                                showToast(3, '系统提示', reslut.Message);
                            }
                        },
                        error: function(request) {
                            showToast(3, '系统提示', ' 操作失败!');
                        }
                    });

                });
            });
        };

    </script>
}
